package com.ip;

import com.graphics.*;
import com.os.ObjectSprite;
import java.util.ArrayList;
import java.util.HashMap;
import mapgenerator.Main;

/**
 *
 * @author Sajarwo Anggai
 */
public class PatternContainer {

    private ArrayList listImagePatternMapping;
    private ArrayList listImagePattern;
    private HashMap<String, ImageIdentity> patternMappingContainer;
    private Main comp;

    public PatternContainer(Main comp) {
        this.comp = comp;
        listImagePatternMapping = new ArrayList();
        listImagePattern = new ArrayList();
        patternMappingContainer = new HashMap();
    }

    public synchronized void addImage(String imageName, int rows, int cols, boolean asSprite) {
        if (listImagePatternMapping.contains(imageName)) {
            System.out.println("\n Object sudah ada = " + imageName);
        } else {
            ImageIdentity imageIdentity = new ImageIdentity(comp, imageName, rows, cols);
            imageIdentity.setSprite(asSprite);
            listImagePatternMapping.add(imageName);
            listImagePattern.add(imageIdentity);
            patternMappingContainer.put(imageName, imageIdentity);
        }
    }

    public void clearAll() {
        listImagePatternMapping.clear();
        listImagePattern.clear();
        patternMappingContainer.clear();
    }

    public ArrayList getListImagePatternMapping() {
        return listImagePatternMapping;
    }

    public ArrayList getListImagePattern() {
        return listImagePattern;
    }

    public ImageIdentity getPattern(String imageName) {
        return patternMappingContainer.get(imageName);
    }

    public void setListImage(int index, String imageName) {
        System.out.println("Index = " + index + " location=" + imageName);
        try {
            String tmploc = (String) listImagePatternMapping.get(index);
            listImagePatternMapping.set(index, imageName);
            ImageIdentity imageIdentity = patternMappingContainer.get(tmploc);
            imageIdentity.setName(imageName);
            patternMappingContainer.remove(tmploc);
            patternMappingContainer.put(imageName, imageIdentity);
        } catch (Exception e) {
        }
    }

    public synchronized void remove(String imageName) {
        try {
            int index = listImagePatternMapping.indexOf(imageName);
            listImagePatternMapping.remove(imageName);
            eraseSprite(listImagePattern);
            listImagePattern.remove(index);
            patternMappingContainer.remove(imageName);
        } catch (Exception e) {
        }
    }

    private void eraseSprite(ArrayList errase) {
        ArrayList al = comp.osContainer.getSprites();
        for (int i = 0; i < al.size(); i++) {
            ObjectSprite os = (ObjectSprite) al.get(i);
            if (errase.contains(os.getOther())) {
                os.setOther(null);
            }
            if (errase.contains(os.getLeft())) {
                os.setLeft(null);
            }
            if (errase.contains(os.getRight())) {
                os.setRight(null);
            }
            if (errase.contains(os.getUp())) {
                os.setUp(null);
            }
            if (errase.contains(os.getDown())) {
                os.setDown(null);
            }
        }
    }
}
